package com.yyfax.pay.config.data.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.yyfax.commons.json.JacksonUtil;
import com.yyfax.framework.service.CacheService;
import com.yyfax.framework.service.impl.BaseServiceImpl;
import com.yyfax.pay.common.constants.CacheName;
import com.yyfax.pay.config.data.dao.PaymentQuotaDao;
import com.yyfax.pay.config.data.service.PaymentQuotaService;
import com.yyfax.pay.config.model.po.PaymentQuotaPO;

@Service
public class PaymentQuotaServiceImpl extends BaseServiceImpl implements PaymentQuotaService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private PaymentQuotaDao paymentQuotaDao;

	public PaymentQuotaServiceImpl(@Qualifier("payCacheService") CacheService cacheService) {
		super(cacheService);
	}

	@Override
	public int save(PaymentQuotaPO config) throws Exception {
		if (config == null) {
			logger.error("新增商户支付限额信息传入参数为空返回-1");
			return -1;
		}
		clearCache(CacheName.PAYMENT_QUOTA);
		return paymentQuotaDao.insert(config);
	}

	@Override
	public int modify(PaymentQuotaPO config) throws Exception {
		if (config == null) {
			logger.error("修改商户支付限额信息传入参数为空返回-1");
			return -1;
		}
		// 更新数据刷新缓存
		clearCache(CacheName.PAYMENT_QUOTA);

		return paymentQuotaDao.update(config);
	}

	@Override
	public List<PaymentQuotaPO> queryByMerAndPay(String merchantCode, String paymentCode) throws Exception {
		if (StringUtils.isBlank(merchantCode) || StringUtils.isBlank(paymentCode)) {
			logger.error("根据paymentCode，paymentCode获取商户支付限额信息，有参数为空，传入merchantCode为：{}，paymentCode为：{}", merchantCode, paymentCode);
			return null;
		}

		String json = getCache(CacheName.PAYMENT_QUOTA);
		if (StringUtils.isNotBlank(json)) {
			List<PaymentQuotaPO> list = (List<PaymentQuotaPO>) JacksonUtil.fromJson(json, List.class, PaymentQuotaPO.class);
			List<PaymentQuotaPO> paymentList = new ArrayList<PaymentQuotaPO>();
			for (PaymentQuotaPO po : list) {
				if (merchantCode.equals(po.getMerchantCode()) && paymentCode.equals(po.getPaymentCode())) {
					paymentList.add(po);
				}
			}
			if (!paymentList.isEmpty() && paymentList != null) {
				return paymentList;
			}
		} else {
			queryForList();
		}

		logger.info("内存为空,从数据库获取");
		return paymentQuotaDao.selectByMerAndPay(merchantCode, paymentCode);
	}

	@Override
	public PaymentQuotaPO query(String merchantCode, String paymentCode, String bankCode, String subjectType, String payMode) throws Exception {

		if (StringUtils.isBlank(merchantCode) || StringUtils.isBlank(paymentCode) || StringUtils.isBlank(bankCode)) {
			logger.error("根据paymentCode，paymentCode，bankCode获取商户支付限额信息，有参数为空，传入merchantCode为：{}，paymentCode为：{}，bankCode：{}", merchantCode,
					paymentCode, bankCode);
			return null;
		}
		String json = getCache(CacheName.PAYMENT_QUOTA);
		List<PaymentQuotaPO> list = null;
		if (StringUtils.isNotBlank(json)) {
			list = (List<PaymentQuotaPO>) JacksonUtil.fromJson(json, List.class, PaymentQuotaPO.class);

		} else {
			logger.info("内存为空,从数据库获取");
			list = queryForList();
		}

		if (list == null) {
			return null;
		}

		for (PaymentQuotaPO po : list) {
			if (merchantCode.equals(po.getMerchantCode()) && paymentCode.equals(po.getPaymentCode()) && bankCode.equals(po.getBankCode())
					&& subjectType.equals(po.getSubjectType()) && payMode.equals(po.getPayMode())) {
				return po;
			}
		}

		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PaymentQuotaPO> queryForList() throws Exception {
		// 从缓存读取
		List<PaymentQuotaPO> pos = null;
		String json = getCache(CacheName.PAYMENT_QUOTA);
		if (StringUtils.isNotBlank(json)) {
			pos = (List<PaymentQuotaPO>) JacksonUtil.fromJson(json, List.class, PaymentQuotaPO.class);
			return pos;
		}
		logger.info("缓存数据为空从数据库获取查询所有商户支付限额信息");
		pos = paymentQuotaDao.selectForList();
		if (pos != null && !pos.isEmpty()) {
			cacheService.set(CacheName.PAYMENT_QUOTA, JacksonUtil.toJson(pos));
		}
		return pos;
	}

	@Override
	public PaymentQuotaPO queryById(int id) throws Exception {
		if (id <= 0) {
			logger.error("根据id商户支付限额信息传入id为{},id错误返回空", id);
			return null;
		}

		return paymentQuotaDao.selectById(id);
	}
}
